$('[data-toggle="tooltip"]').tooltip() // 开启

$(function(){
    $(".tooltips").tooltip();//显示title
});

// 模态框定义
let dialog_normal_user_name_null =  new mModal({ // 登录用户失败
    content:"请输入用户名后搜索",
    title:"输入错误",
    confirm:function () {
        dialog_normal_user_name_null.close()
    }
})
// 失败方法
function openModalDialog(dg,title,content) {
    dg =  new mModal({ // 登录用户失败
        content:content,
        title:title,
        confirm:function () {
            dg.close()
        }
    })
}


$(function (){

    $("#btn_user_search_user").click(function () {
        let inputtedUserName = $("#input_search_user").val()
        if (inputtedUserName !== ""){
            $.ajax({
                url:"/admin/controls/normalUser/searchNormalUser",
                type:"POST",
                data:JSON.stringify({
                    "userName":inputtedUserName,
                }),
                contentType:"application/json; charset=UTF-8",
                dataType:"json",
                success:function (data,status,xhr) {
                    var tableBody_element = $("#tbody_user_list")
                    tableBody_element.html("")
                    if (data.code === 200){
                        var userList = data.data
                        for (var i = 0; i <= userList.length; i++){
                            // 创建节点对象
                            var ele_tr = document.createElement("tr");
                            var ele_td_uid = document.createElement("td");
                            var ele_td_userName = document.createElement("td");
                            var ele_td_PassWord = document.createElement("td");

                            // 写入文本
                            ele_td_uid.innerText = userList[i].uid;
                            ele_td_userName.innerText = userList[i].username;
                            ele_td_PassWord.innerText = userList[i].password;

                            // 追加节点
                            ele_tr.append(ele_td_uid,ele_td_userName,ele_td_PassWord);
                            $("#tbody_user_list").append(ele_tr);

                        }
                    } else {
                        alert("搜索失败")
                    }
                }
            })
        } else {
            dialog_normal_user_name_null.renderDom()
        }
    })

    // 弹出模态框定义
    $("#btn_add_user").click(function () {
        $("#model_add_user_dialog").modal()
    })
    $("#btn_edt_user").click(function () {
        $("#model_edt_user_dialog").modal()
    })
    $("#btn_del_user").click(function () {
        $("#model_del_user_dialog").modal()
    })

    // 当查询用户按钮被单击
    $("#btn_user_edt_search_uid").click(function () {
        var inputAddEdtUid = $("#input_add_edt_uid")
        if (inputAddEdtUid.val() != null) {
            $.ajax({
                url:"/admin/controls/normalUser/getUserInfoByUid",
                type:"POST",
                data:JSON.stringify({
                    "uid":inputAddEdtUid.val(),
                }),
                contentType:"application/json; charset=UTF-8",
                dataType:"json",
                success:function (data,status,xhr) {
                    if (data.code ===200){

                    } else {

                    }
                }
            })
        }

    })

    // 模态框提交按钮事件
    // 添加模态框提交按钮
    $("#btn_user_add_commit").click(function () {
        let btnUserAddCommit = $("#btn_user_add_commit")
        btnUserAddCommit.prop("disabled",true)
        btnUserAddCommit.text("提交中")

        // 获取文本数据
        let inputtedUserName = $("#input_add_user_username");
        let inputtedPassWord = $("#input_add_user_password");
        let inputtedGender = $("#input_add_user_gender");

        if (inputtedUserName.val() != null && inputtedPassWord.val() != null && inputtedGender.val() != null){
            $.ajax({
                url:"/admin/controls/normalUser/addNormalUser",
                type:"POST",
                data:JSON.stringify({
                    "userName":inputtedUserName.val(),
                    "password":inputtedPassWord.val(),
                    "sex":inputtedGender.val()
                }),
                contentType:"application/json; charset=UTF-8",
                dataType:"json",
                success:function (data,status,xhr) {
                    if (data.code === 200) {
                        let insertedUserInfo = data.insertedUser
                        let uid = insertedUserInfo.uid
                        let userName = insertedUserInfo.username
                        let dg = null;
                        alert("用户 "+userName+" 添加成功！");
                        $("#model_add_user_dialog").modal("hide")
                    } else {
                        alert("对不起添加失败")
                        openModalDialog("添加失败","对不起添加失败,Error Caused：<br/>"+data.msg)
                    }

                    btnUserAddCommit.text("提交更改")
                    btnUserAddCommit.prop("disabled",false)
                }
            })
        }


    })


})